package com.newtouch.bxzs.policy.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * 调用外部接口后数据分页处理
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/20 17:48</td>
 * <td>linhongxi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author linhongxi
 * @version 1.0.0
 * @since 1.0.0
 */
public class PolicyListPageUtils<T> {
//     定义数组变量，data.length表示数组容量capacity
//    List<T> policyListPage;
//    Integer pageSize;
//    Integer currentPage;

    public PolicyListPageUtils(){

    }
    public Page<T> policyListByPage(List<T> policyListPage, Integer pageSize, Integer currentPage){
        int totalcount = policyListPage.size();
        int pagecount = 0;
        List<T> subList;
        Page<T> subListByPage = new Page<>();
        if (totalcount > pageSize) {
            pagecount = totalcount / pageSize + 1;
        } else {
            pagecount = 1;
        }
        if (totalcount == pageSize) {
            subList = policyListPage.subList((currentPage - 1) * pageSize, pageSize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = policyListPage.subList((currentPage - 1) * pageSize, totalcount);
            } else {
                subList = policyListPage.subList((currentPage - 1) * pageSize, pageSize * (currentPage));
            }
        }
        subListByPage.setRecords(subList);
        subListByPage.setTotal(totalcount);
        subListByPage.setCurrent(currentPage);
        subListByPage.setSize(pageSize);
        return subListByPage;
    }
}
